
***************************************************
* Table 2
***************************************************

***************************************************
* Table 2_1
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

g e=1
g b_year=year(foed_dag)
g b_month=month(foed_dag)

collapse wage sa (sum) e, by(pnr age_q year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************


eststo clear
local model=0
foreach y of varlist sa {
local num=0
forvalues year=2000/2006 {  
local num=`num'+1

preserve
collapse `y' if age_q>=21 & age_q<29 & year==`year', by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

sum `y' if age_month_q==24.11 & year==`year'
local initial=round(r(mean), 0.01)
di `initial'

preserve
keep if year==`year'      
keep if age_q>=21 & age_q<29

if `y'==sa   local title="Social Assistance"
if `y'==sa   local C=1

local spec_name="2nd order polynomial spline"
local spec="d age_q0 age_q0_2      dXage_q0 dXage_q0_2"

local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"

eststo model`model': reg `y' `spec' [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'
regsave d using $tables/tab2_1 , addlabel(samplesize, `obs', N_clust,`N_clust',outcome,`y',spec,`spec_name',year,`year',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

restore
}
} 

***************************************************
* Table 2_2
***************************************************

use "$workdata/cohort", clear /* see $data_do/cohort.do */

keep if b_year>=1975 & b_year<=1981
keep if age_q>=21 & age_q<29
g e=1

collapse sa (sum) e, by(pnr age_q b_year) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=24.12 & age_month_q<=25.04"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************


eststo clear
local model=0
foreach y of varlist sa {
local num=0
forvalues year=1975/1981 {  
local cal_year=`year'+25 


preserve
collapse `y' if age_q>=21 & age_q<29 & b_year==`year', by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

sum `y' if age_month_q==24.11 & b_year==`year'
local initial=round(r(mean), 0.01)
di `initial'

preserve
keep if b_year==`year'    
keep if age_q>=21 & age_q<29

if `y'==sa   local title="Social Assistance"
local min_wage=0.49
local max_wage=0.59
local min_sa  =0.1
local max_sa  =0.20
local num=`num'+1

local spec_name="2nd order poly. spline"

local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
reg `y' `spec' [aw=e] , vce(cluster pnr)
eststo model`model': reg `y' `spec' [aw=e] if resp==0 , vce(cluster pnr)
local N_clust=e(N_clust)
local pct_of_initial= round(((round(_b[d],0.001)/`initial')*100), 0.1)
di `pct_of_initial'
regsave d using $tables/tab2_2 , addlabel(samplesize, `obs', N_clust,`N_clust',outcome,`y',spec,`spec_name',birth_year,`year',regnum,`model',initial,`initial',pct_of_initial,`pct_of_initial') `repl_app'

restore
}
}  

use "$restat/table2_1" , clear
g sample="baseline"
append using "$restat/table2_2"
replace sample="cohort" if sample==""
save "$restat/table2" , replace

***************************************************
* end: Table 2
***************************************************
